const express = require('express');
const router = express.Router();
const dbConfig = require('../db.js')
const mysql = require('mysql');
const sql = require('../sql.js')
var path = require("path");
const multiparty = require('multiparty');
const fs = require('fs');
var connection = mysql.createConnection(dbConfig)
connection.connect();


const dir = path.resolve(__dirname, '..')
const uploadDir = path.resolve(dir, 'upload')

router.get('/getUserPic', (req, res, next) => {
	let id = req.query.user_id;
	let pic = '';
	connection.query(sql.upload.getUserPic, id, (err, result, fields) => {
		if (err) {
			console.log(err)
		}

		if (result[0].pic == null) {
			return res.status(200).send(pic)
		}
		pic = path.resolve(uploadDir, result[0].pic);
		return res.status(200).sendFile(pic)
	})
})
router.get('/getUserPicDir', (req, res, next) => {
	let id = req.query.user_id;
	let pic = '';
	connection.query(sql.upload.getUserPic, id, (err, result, fields) => {
		if (err) {
			console.log(err)
		}
		pic = result[0].pic
		return res.status(200).send(pic)
	})
})
router.post('/uploadUserPic', (req, res, next) => {

	let form = new multiparty.Form();
	form.uploadDir = 'upload'

	form.parse(req, (err, fields, files) => {

		let id = fields.id[0];
		let path = files.file[0].path.split('\\')[1];
		let u = [path, id]
		connection.query(sql.upload.uploadUserPic, u, (err, result, fields) => {
			if (err) {
				console.log(err)
				return res.send(500)
			}
			res.sendStatus(200)
		})
	})

})

router.get('/deleteUserPic', (req, res, next) => {
	let picDir = req.query.oldPic;
	pic = path.resolve(uploadDir, picDir);
	fs.unlink(pic, (err) => {
		if (err) {
			console.log(err)
		} else {
			res.send('ok')
		}

	})

})


router.use('/getCarPic', (req, res, next) => {
	let car_id = req.body.car_id;
	let pic = '';
	connection.query(sql.upload.getCarPic, car_id, (err, result, fields) => {
		if (err) {
			console.log(err)
		}
		if (!result[0].pic) {
			return res.status(200).send(pic)
		}else{
			pic = path.resolve(uploadDir, result[0].pic);
			return res.status(200).sendFile(pic)
		}
		
	})
	

})

router.use('/getCarPicDir', (req, res, next) => {
	let id = req.query.car_id;
	let pic = '';
	connection.query(sql.upload.getCarPic, id, (err, result, fields) => {
		if (err) {
			console.log(err)
		}
		pic = result[0].pic
		res.status(200).send(pic)
	})
})

router.use('/uploadCarPic', (req, res, next) => {
	let form = new multiparty.Form();
	form.uploadDir = 'upload'

	form.parse(req, (err, fields, files) => {

		let id = fields.id[0];
		let path = files.file[0].path.split('\\')[1];
		let car = [path, id]
		connection.query(sql.upload.uploadCarPic, car, (err, result, fields) => {
			if (err) {
				console.log(err)
				return res.sendStatus(500)
			}
			res.sendStatus(200)
		})
	})
})
router.get('/deleteCarPic', (req, res, next) => {
	let picDir = req.query.oldPic;
	pic = path.resolve(uploadDir, picDir);
	fs.unlink(pic, (err) => {
		if (err) {
			console.log(err)
		} else {
			res.send('ok')
		}

	})

})

module.exports = router;
